<template>
  <el-container class="layout-container">
    <!-- 头部导航栏 -->
    <el-header class="header" height="70px">
      <div class="header-content">
        <div class="logo-area">
          <h1 class="logo" @click="$router.push('/')">🏪 在线商城</h1>
          <el-menu
            mode="horizontal"
            :default-active="activeMenu"
            @select="handleMenuSelect"
            class="main-menu"
          >
            <el-menu-item index="home">首页</el-menu-item>
            <el-menu-item v-for="category in categories" :key="category.key" :index="category.key">
              {{ category.icon }} {{ category.name }}
            </el-menu-item>
          </el-menu>
        </div>
        
        <div class="header-right">
          <el-input
            v-model="searchKeyword"
            placeholder="搜索商品..."
            class="search-input"
            @keyup.enter="handleSearch"
            size="small"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
          
          <div class="user-info" v-if="currentUser">
            <el-dropdown>
              <span class="user-name">
                <el-icon><User /></el-icon>
                {{ currentUser.username }}
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
          
          <div v-else class="auth-buttons">
            <el-button @click="$router.push('/login')" size="small" link>登录</el-button>
            <el-divider direction="vertical" />
            <el-button type="primary" @click="$router.push('/register')" size="small" plain>注册</el-button>
          </div>
        </div>
      </div>
    </el-header>

    <el-container>
      <!-- 侧面导航栏 -->
      <el-aside width="220px" class="sidebar">
        <el-menu
          :default-active="activeCategory"
          class="category-menu"
          @select="handleCategorySelect"
          router
        >
          <el-menu-item index="/">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-divider />
          <div class="menu-title">商品分类</div>
          <el-menu-item 
            v-for="category in categories" 
            :key="category.key" 
            :index="`/category/${category.key}`"
          >
            <span style="margin-right: 10px">{{ category.icon }}</span>
            <span>{{ category.name }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-main class="main-content">
        <slot />
      </el-main>
    </el-container>

    <!-- 悬浮购物车按钮 -->
    <div class="floating-cart">
      <el-badge :value="cartCount" :hidden="cartCount === 0" class="floating-cart-badge">
        <el-button 
          @click="$router.push('/cart')" 
          :icon="ShoppingCart" 
          type="danger"
          circle
          size="large"
          class="floating-cart-button"
        />
      </el-badge>
    </div>
  </el-container>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, ShoppingCart, HomeFilled, User } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const searchKeyword = ref('')

const categories = [
  { name: '美妆', key: 'makeup', icon: '💄' },
  { name: '服饰', key: 'clothing', icon: '👗' },
  { name: '数码', key: 'digital', icon: '📱' },
  { name: '电器', key: 'appliance', icon: '🔌' },
  { name: '生鲜', key: 'fresh', icon: '🍎' },
  { name: '户外', key: 'outdoor', icon: '🏕️' }
]

const activeMenu = computed(() => {
  if (route.path === '/') return 'home'
  if (route.path.startsWith('/category/')) return route.params.category
  return 'home'
})

const activeCategory = computed(() => {
  return route.path
})

const currentUser = ref(null)

const cartCount = computed(() => {
  const savedCart = localStorage.getItem('cart')
  if (savedCart) {
    const items = JSON.parse(savedCart)
    return items.reduce((sum, item) => sum + item.quantity, 0)
  }
  return 0
})

const checkUserLogin = () => {
  const userStr = localStorage.getItem('user')
  if (userStr) {
    currentUser.value = JSON.parse(userStr)
  }
}

const handleLogout = () => {
  localStorage.removeItem('user')
  localStorage.removeItem('token')
  currentUser.value = null
  ElMessage.success('已退出登录')
  router.push('/')
}

onMounted(() => {
  checkUserLogin()
})

watch(() => route.path, () => {
  checkUserLogin()
})

const handleMenuSelect = (key) => {
  if (key === 'home') {
    router.push('/')
  } else {
    router.push(`/category/${key}`)
  }
}

const handleCategorySelect = (path) => {
  router.push(path)
}

const handleSearch = () => {
  if (searchKeyword.value) {
    router.push(`/category/all?search=${searchKeyword.value}`)
  }
}

// 监听路由变化，更新搜索框
watch(() => route.query.search, (newVal) => {
  if (newVal) {
    searchKeyword.value = newVal
  }
})
</script>

<style scoped>
.layout-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  max-width: 1600px;
  margin: 0 auto;
  box-sizing: border-box;
}

.header {
  background: white;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  padding: 0;
}

.header-content {
  height: 100%;
  max-width: 1600px;
  width: 100%;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
  box-sizing: border-box;
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 30px;
}

.logo {
  font-size: 24px;
  color: #409eff;
  cursor: pointer;
  margin: 0;
  white-space: nowrap;
}

.main-menu {
  border-bottom: none;
}

.search-input {
  width: 200px;
  margin-right: 15px;
}

.user-info {
  margin-left: 15px;
}

.user-name {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  color: #409eff;
  font-weight: 500;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-name:hover {
  background-color: #ecf5ff;
}

.auth-buttons {
  margin-left: 15px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.cart-badge {
  margin-right: 10px;
}

.sidebar {
  background: white;
  border-right: 1px solid #e4e7ed;
  margin-right: 20px;
  border-radius: 10px;
  margin-top: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.category-menu {
  border-right: none;
  padding: 20px 0;
}

.menu-title {
  padding: 10px 20px;
  color: #909399;
  font-size: 14px;
  font-weight: bold;
}

.main-content {
  background: white;
  border-radius: 10px;
  margin-top: 20px;
  margin-bottom: 20px;
  margin-right: 20px;
  padding: 30px;
  min-height: calc(100vh - 110px);
  max-width: 100%;
  box-sizing: border-box;
}

.floating-cart {
  position: fixed;
  right: 30px;
  bottom: 30px;
  z-index: 1000;
  animation: float 2s ease-in-out infinite;
}

.floating-cart-button {
  width: 60px;
  height: 60px;
  font-size: 28px;
  box-shadow: 0 4px 20px rgba(245, 108, 108, 0.4);
  transition: all 0.3s;
  border: none;
}

.floating-cart-button:hover {
  transform: scale(1.15) !important;
  box-shadow: 0 6px 30px rgba(245, 108, 108, 0.6);
  animation-play-state: paused;
}

.floating-cart-badge :deep(.el-badge__content) {
  top: 8px;
  right: 8px;
  font-size: 12px;
  min-width: 20px;
  height: 20px;
  line-height: 20px;
  padding: 0 6px;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px);
  }
  50% {
    transform: translateY(-10px);
  }
}
</style>

